{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-12T02:12:14.184654Z",
     "start_time": "2019-04-12T02:12:14.178671Z"
    }
   },
   "outputs": [],
   "source": [
    "# coding=utf-8"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 基本参数设置"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "设置正交试验设计中每个样本的变量情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-12T02:12:14.199621Z",
     "start_time": "2019-04-12T02:12:14.187648Z"
    }
   },
   "outputs": [],
   "source": [
    "# 所有水平列表\n",
    "LEVEL_LIST = [\"A\", \"B\", \"C\", \"D\", \"E\", \"F\"]\n",
    "LEVEL_NAME = [\"颜值\", \"成绩\", \"爱好\", \"年龄\", \"身高\", \"家庭\"]\n",
    "# 所有因素列表\n",
    "FACTORY_LIST = ['A1', 'A2', 'A3', 'B1', 'B2', 'B3',\n",
    "                'C1', 'C2', 'C3', 'D1', 'D2', 'D3',\n",
    "                'E1', 'E2', 'E3', 'F1', 'F2', 'F3']\n",
    "FACTORY_NAME = [\"高\", \"一般\", \"低\", \"优秀\", \"一般\", \"较差\",\n",
    "                \"相似\", \"互补\", \"毫不相关\", \"更大\", \"同级\", \"更小\",\n",
    "                \"身高\", \"相似\", \"更低\", \"更好\", \"一般\", \"不那么好\"]\n",
    "FACTORY_LEVEL_NAME = [\"颜值\", \"颜值\", \"颜值\", \"成绩\", \"成绩\", \"成绩\",\n",
    "                      \"爱好\", \"爱好\", \"爱好\", \"年龄\", \"年龄\", \"年龄\",\n",
    "                      \"身高\", \"身高\", \"身高\", \"家庭\", \"家庭\", \"家庭\"]\n",
    "# 正交试验设计结构\n",
    "CASE_STRUCTURE = ['A2B2C1D2E2F3', 'A1B3C3D2E2F2', 'A3B1C1D2E1F3', 'A3B1C3D2E3F2', 'A2B1C2D3E1F2', 'A1B2C1D3E1F2',\n",
    "                  'A1B1C2D1E2F3', 'A2B1C3D3E2F1', 'A1B2C3D3E3F3', 'A3B3C2D3E3F3', 'A2B3C1D1E3F2', 'A2B2C2D2E3F1',\n",
    "                  'A3B2C3D1E1F1', 'A3B3C1D3E2F1', 'A1B1C1D1E3F1', 'A3B2C2D1E2F2', 'A1B3C2D2E1F1', 'A2B3C3D1E1F3']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "给定正交试验样本的得分情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-12T02:12:14.208591Z",
     "start_time": "2019-04-12T02:12:14.201628Z"
    }
   },
   "outputs": [],
   "source": [
    "MARK = [3, 4, 8, 2, 1, 2, 4, 7, 6, 4, 7, 6, 2, 7, 6, 10, 3, 9]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 联合分析"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.导入包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-12T02:12:30.569011Z",
     "start_time": "2019-04-12T02:12:14.210585Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import statsmodels.api as sm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.生成初始化个案表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-12T02:12:30.620723Z",
     "start_time": "2019-04-12T02:12:30.569848Z"
    }
   },
   "outputs": [],
   "source": [
    "columns_list = [\"Mark\"]\n",
    "columns_list.extend(FACTORY_LIST)\n",
    "ConjointDummyDF = pd.DataFrame(np.zeros((len(CASE_STRUCTURE), len(FACTORY_LIST) + 1)), columns=columns_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.生成个案得分对应表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-12T02:12:30.630706Z",
     "start_time": "2019-04-12T02:12:30.625702Z"
    }
   },
   "outputs": [],
   "source": [
    "markDF = pd.DataFrame({\"Level\": CASE_STRUCTURE, \"Mark\": MARK})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.将正交设计分布写入个案表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-12T02:12:30.671587Z",
     "start_time": "2019-04-12T02:12:30.633679Z"
    }
   },
   "outputs": [],
   "source": [
    "for index, row in markDF.iterrows():\n",
    "    factory = []\n",
    "    for i in range(0, len(LEVEL_LIST)):\n",
    "        factory.append(markDF[\"Level\"].loc[index][i * 2:i * 2 + 2])\n",
    "    ConjointDummyDF.loc[index, factory] = 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5.将得分写入个案表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-12T02:12:30.686535Z",
     "start_time": "2019-04-12T02:12:30.672572Z"
    }
   },
   "outputs": [],
   "source": [
    "ConjointDummyDF[\"Mark\"] = markDF[\"Mark\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "6.回归计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-12T02:12:30.777298Z",
     "start_time": "2019-04-12T02:12:30.687532Z"
    }
   },
   "outputs": [],
   "source": [
    "# 计算回归分析的X和Y\n",
    "X = sm.add_constant(ConjointDummyDF[FACTORY_LIST])\n",
    "Y = ConjointDummyDF[\"Mark\"]\n",
    "# 执行回归分析\n",
    "linearRegression = sm.OLS(Y, X).fit()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "7.计算效用估计表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-12T02:12:30.787296Z",
     "start_time": "2019-04-12T02:12:30.778290Z"
    }
   },
   "outputs": [],
   "source": [
    "# 生成按组存储的回归系数\n",
    "level_utilities = []\n",
    "for level in LEVEL_LIST:\n",
    "    temp_utility = []\n",
    "    for factory in FACTORY_LIST:\n",
    "        if factory[0] == level:\n",
    "            temp_utility.append(linearRegression.params[factory])\n",
    "    level_utilities.append(temp_utility)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-12T02:12:30.793279Z",
     "start_time": "2019-04-12T02:12:30.789264Z"
    }
   },
   "outputs": [],
   "source": [
    "# 计算各水平的重要性(回归系数极差)\n",
    "importance = []\n",
    "for item in level_utilities:\n",
    "    importance.append(max(item) - min(item))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-12T02:12:30.802226Z",
     "start_time": "2019-04-12T02:12:30.795245Z"
    }
   },
   "outputs": [],
   "source": [
    "# 计算各水平的重要性百分比\n",
    "relative_importance = []\n",
    "for item in importance:\n",
    "    relative_importance.append(100 * round(item / sum(importance), 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-12T02:12:30.820186Z",
     "start_time": "2019-04-12T02:12:30.803223Z"
    },
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "# 计算各因素的平均得分\n",
    "meanMark = []\n",
    "for i in ConjointDummyDF.columns[1:]:\n",
    "    newMeanMark = ConjointDummyDF[\"Mark\"].loc[ConjointDummyDF[i] == 1].mean()\n",
    "    meanMark.append(newMeanMark)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-12T02:12:30.829154Z",
     "start_time": "2019-04-12T02:12:30.821176Z"
    }
   },
   "outputs": [],
   "source": [
    "# 计算各因素平均得分\n",
    "totalMeanMark = sum(meanMark) / len(meanMark)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-12T02:12:30.837308Z",
     "start_time": "2019-04-12T02:12:30.831158Z"
    }
   },
   "outputs": [],
   "source": [
    "# 计算各因素效用值\n",
    "utility = []\n",
    "for i in range(len(meanMark)):\n",
    "    name = sorted(FACTORY_LIST)[i]\n",
    "    utility.append(meanMark[i] - totalMeanMark)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-12T02:12:30.872040Z",
     "start_time": "2019-04-12T02:12:30.838131Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>水平</th>\n",
       "      <th>因素</th>\n",
       "      <th>效用值</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>颜值</td>\n",
       "      <td>高</td>\n",
       "      <td>-0.888889</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>颜值</td>\n",
       "      <td>一般</td>\n",
       "      <td>0.444444</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>颜值</td>\n",
       "      <td>低</td>\n",
       "      <td>0.444444</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>成绩</td>\n",
       "      <td>优秀</td>\n",
       "      <td>-0.388889</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>成绩</td>\n",
       "      <td>一般</td>\n",
       "      <td>-0.222222</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>成绩</td>\n",
       "      <td>较差</td>\n",
       "      <td>0.611111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>爱好</td>\n",
       "      <td>相似</td>\n",
       "      <td>0.444444</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>爱好</td>\n",
       "      <td>互补</td>\n",
       "      <td>-0.388889</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>爱好</td>\n",
       "      <td>毫不相关</td>\n",
       "      <td>-0.055556</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>年龄</td>\n",
       "      <td>更大</td>\n",
       "      <td>1.277778</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>年龄</td>\n",
       "      <td>同级</td>\n",
       "      <td>-0.722222</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>年龄</td>\n",
       "      <td>更小</td>\n",
       "      <td>-0.555556</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>身高</td>\n",
       "      <td>身高</td>\n",
       "      <td>-0.888889</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>身高</td>\n",
       "      <td>相似</td>\n",
       "      <td>0.777778</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>身高</td>\n",
       "      <td>更低</td>\n",
       "      <td>0.111111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>家庭</td>\n",
       "      <td>更好</td>\n",
       "      <td>0.111111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>家庭</td>\n",
       "      <td>一般</td>\n",
       "      <td>-0.722222</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>家庭</td>\n",
       "      <td>不那么好</td>\n",
       "      <td>0.611111</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    水平    因素       效用值\n",
       "0   颜值     高 -0.888889\n",
       "1   颜值    一般  0.444444\n",
       "2   颜值     低  0.444444\n",
       "3   成绩    优秀 -0.388889\n",
       "4   成绩    一般 -0.222222\n",
       "5   成绩    较差  0.611111\n",
       "6   爱好    相似  0.444444\n",
       "7   爱好    互补 -0.388889\n",
       "8   爱好  毫不相关 -0.055556\n",
       "9   年龄    更大  1.277778\n",
       "10  年龄    同级 -0.722222\n",
       "11  年龄    更小 -0.555556\n",
       "12  身高    身高 -0.888889\n",
       "13  身高    相似  0.777778\n",
       "14  身高    更低  0.111111\n",
       "15  家庭    更好  0.111111\n",
       "16  家庭    一般 -0.722222\n",
       "17  家庭  不那么好  0.611111"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tableUtilities = pd.DataFrame({\"水平\": FACTORY_LEVEL_NAME, \"因素\": FACTORY_NAME, \"效用值\": utility})\n",
    "tableUtilities"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "8.生成水平重要性表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-12T02:12:30.884008Z",
     "start_time": "2019-04-12T02:12:30.873037Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>水平</th>\n",
       "      <th>重要性</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>颜值</td>\n",
       "      <td>16.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>成绩</td>\n",
       "      <td>12.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>爱好</td>\n",
       "      <td>10.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>年龄</td>\n",
       "      <td>24.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>身高</td>\n",
       "      <td>20.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>家庭</td>\n",
       "      <td>16.3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   水平   重要性\n",
       "0  颜值  16.3\n",
       "1  成绩  12.2\n",
       "2  爱好  10.2\n",
       "3  年龄  24.5\n",
       "4  身高  20.4\n",
       "5  家庭  16.3"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tableImportance = pd.DataFrame({\"水平\": LEVEL_NAME, \"重要性\": relative_importance})\n",
    "tableImportance"
   ]
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
